import { merge } from "lodash-es";
import { $http } from "@/utils/customerHttp.js";
export default merge({
  namespaced: true,
  state: {},
  mutations: {},
  getters: {},
  actions: {
    // 与建立客服连接
    async conService({ dispatch }, params) {
      console.log("连接");
      console.log('params====',params)
      const { data } = await $http.post("/admin/event/notice", {
        visiter_id: params.visiter_id,
        visiter_name: params.visiter_name,
        business_id: params.business_id,
        from_url: "",
        avatar: "/assets/images/index/avatar-red2.png",
        groupid: 0
      });
      return data;
    },
    // 获取历史记录
    async getHistory({ dispatch }, params) {
      return dispatch(
        "toggleLoading",
        {
          key: "service.conService",
          loading: async () => {
            const { data } = await $http.post("/admin/event/chatdata", {
              hid: "",
              vid: params.vid,
              service_id: params.service_id,
              business_id: params.business_id
            });
            return data;
          }
        },
        {
          root: true
        }
      );
    },
    // 发送信息
    async sendInformation({ dispatch }, params) {
      return dispatch(
        "toggleLoading",
        {
          key: "service.conService",
          loading: async () => {
            const { data } = await $http.post("/admin/event/chat", {
              visiter_id: params.visiter_id,
              content: params.content,
              avatar: "/assets/images/index/avatar-red2.png",
              record: "",
              service_id: params.service_id,
              noswitch: params.service_id
            });
            return data;
          }
        },
        {
          root: true
        }
      );
    },
    // 获得webSocket chanel
    async getChanel({ dispatch }, params) {
      return dispatch(
        "toggleLoading",
        {
          key: "service.getChanel",
          loading: async () => {
            const { data } = await $http.get("/mobile/index/channel", {
              visiter_id: params.visiter_id
            });
            return data;
          }
        },
        {
          root: true
        }
      );
    },
    // 给出已读
    async readed({ dispatch }, params) {
      return dispatch(
        "toggleLoading",
        {
          key: "service.readed",
          loading: async () => {
            const { data } = await http.post("/admin/event/readed", {
              visiter_id: params.visiter_id,
              service_id: params.service_id
            });
            return data;
          }
        },
        {
          root: true
        }
      );
    },
    //图片上传
    async uploadImg({ dispatch }, params) {
      console.log("连接111");
      const { data } = await $http.post("/admin/event/upload", {
        // visiter_id: params.visiter_id,
        // visiter_name: params.visiter_name,
        // business_id: params.business_id,
        // from_url: "",
        // avatar: "/assets/images/index/avatar-red2.png",
        // groupid: 0,

        visiter_id:visiter_id,
        business_id: business_id,
        avatar: pic,
        record: record,
        service_id:service_id

      });
      console.log("data", data);
      return data;
    },
    //发送消息
    async uploadImg({ dispatch }, params) {
      console.log("连接222");
      const { data } = await $http.post("/admin/event/chat", {
        visiter_id:visiter_id,
        content: content,
        business_id: business_id, 
        avatar: pic,record: record, 
        service_id:service_id, 
        noswitch: typeof noswitch == 'undefined' ? 0 : noswitch
    
      });
      console.log("data", data);
      return data;
    }
  }
});
